package com.audaque.springboot.foshanupload.web.esdatademo.controller;

import com.audaque.springboot.foshanupload.web.esdatademo.model.pojo.Blog;
import com.audaque.springboot.foshanupload.web.esdatademo.model.pojo.StudentPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;


/**
 * @Description:用户信息表 管理
 * @Auther:zhong.guobin
 * @DateTime:2023-01-23T02:16:18.507
 */

@Controller
public class MappingAction   {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @PostConstruct
    public boolean createIndexAndPushMapping() {
        if(!elasticsearchRestTemplate.indexExists(Blog.class)){
            IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Blog.class);
            //创建索引
            boolean a = indexOperations.create();
            if (a) {
                //生成映射
                Document mapping = indexOperations.createMapping();
                //推送映射
                boolean b = indexOperations.putMapping(mapping);
                return b;
            } else {
                return a;
            }
        }
        return true;

    }

}
